/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package carpool.services;

import carpool.persistence.HibernateUtil;
import carpool.persistence.fctuc.Users;
import java.beans.*;
import java.io.Serializable;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;

/**
 *
 * @author sdinis
 */
public class FCTUCBean implements Serializable {

    public static final String PROP_SAMPLE_PROPERTY = "sampleProperty";

    private String sampleProperty;

    private PropertyChangeSupport propertySupport;

    public FCTUCBean() {
        propertySupport = new PropertyChangeSupport(this);
    }

    public boolean createAndStoreUser(Users user) {
         try{
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();

        session.beginTransaction();       

        session.save(user);
        session.getTransaction().commit();
        return true;
         } catch (HibernateException ex) {
             return false;
         } finally {
             //nothing
         }
    }

     public Users getUser(int id) {
         Users u = new Users();
         Session session = HibernateUtil.getSessionFactory().getCurrentSession();
         session.beginTransaction();
         u = (Users) session.createQuery("from Users as user where user.id=?")
                 .setInteger(0, id)
                 .uniqueResult();
         session.getTransaction().commit();
         return u;
     }
     
     public String showUser(int id)
     {
         Session session = HibernateUtil.getSessionFactory().getCurrentSession();
         session.beginTransaction();
             String testName = (String) session.createQuery(
                "select name from Test where id = ?")
                .setInteger(0, id)
            .uniqueResult();
             session.getTransaction().commit();

         return testName;
     }


    public String getSampleProperty() {
        return sampleProperty;
    }

    public void setSampleProperty(String value) {
        String oldValue = sampleProperty;
        sampleProperty = value;
        propertySupport.firePropertyChange(PROP_SAMPLE_PROPERTY, oldValue, sampleProperty);
    }


    public void addPropertyChangeListener(PropertyChangeListener listener) {
        propertySupport.addPropertyChangeListener(listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        propertySupport.removePropertyChangeListener(listener);
    }

}
